
/*********************/
/**   当事人选择框   **/
/*********************/
	Ext.onReady(function(){
		Ext.QuickTips.init();
	 var store = new Ext.data.JsonStore({
	        root: 'topics',
	        totalProperty: 'totalCount',
	        idProperty: 'id',
	        remoteSort: true,
	        url: '/app/biz/breakerchoose/breakerchoose!getBreakers',
	        fields: ['id', 'name','entityType','idNo']
	    });

	    var store2 = new Ext.data.JsonStore({
	        fields : ['name','idNo'],
			root   : 'topics'
	    });

	    var entityType = new Ext.form.ComboBox({
						id:'entityType',
		                xtype:'combo',
		                emptyText: '请选择性质',
		                width:100,
		                displayField:'value',
		                fields: ['key', 'value'],
		                forceSelection :true,
		                selectOnFocus :true,
		                typeAhead:true,
		                triggerAction: 'all',
		                store: [["","==不限=="],["1","单位"],["2","个人"]]
		});
		
			var genderRadioGroup = {
		            xtype: 'radiogroup',
		            fieldLabel: '性别',
		            columns: [50,50],
		            items: [
		                {boxLabel: '男', name: 'entity.gender', inputValue: 0, checked: true},
		                {boxLabel: '女', name: 'entity.gender', inputValue: 1}
		            ]
		    };
		    
		    var entityTypeRadioGroup = {
		            xtype: 'radiogroup',
		            fieldLabel: '性质',
		            columns: [50,50],
		            items: [
		                {boxLabel: '单位', name: 'entity.entityType', inputValue: 1, checked: true},
		                {boxLabel: '个人', name: 'entity.entityType', inputValue: 2}
		            ]
		    };
    
		var inputWin = null;//增加当事人窗口
		var simple = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        url:'/app/biz/breakermanage/breaker!save?method=add',
        
        frame:true,
        title: '增加当事人',
        bodyStyle:'padding:5px 5px 0 0',
        width: 350,
        defaults: {width: 230},
        defaultType: 'textfield',

        items: [{
                fieldLabel: '当事人名称',
                name: 'entity.breakerName',
                allowBlank:false
            },entityTypeRadioGroup,{
                fieldLabel: '编号',
                name: 'entity.idNo'
            }, genderRadioGroup, {
                fieldLabel: '单位',
                name: 'entity.org'
            }, {
                fieldLabel: '单位负责人',
                
                name: 'entity.leader'
            }, {
                fieldLabel: '职位',
                name: 'entity.job'
            }, {
                fieldLabel: '地址',
                name: 'entity.address'
            }, {
                fieldLabel: '电话',
                name: 'entity.phone'
            }
        ],

        buttons: [{
            text: '增加',
            handler: function(){
            	simple.getForm().submit({
            	 url:'/app/biz/breakermanage/breaker!save?method=add',
               	 method: 'POST',
               	 success:function(f,action){  
                           inputWin.hide();
                           simple.getForm().reset();
                           store.load({params:{start:0, limit:10}});
                        }

            	});
            }
        },{
            text: '关闭',
            handler: function(){
                       inputWin.hide();
                   }
        }]
    });
           
	    var grid = new Ext.grid.GridPanel({
	        width:450,
	        height:250,
	        ddGroup          : 'secondGridDDGroup',
	        enableDragDrop   : true,
	        store: store,
	        trackMouseOver:true,
	        disableSelection:false,
	        loadMask: true,
	        stripeRows: true,
	        autoExpandColumn:'name',
	        // grid columns
	        columns:[{
	            header: "id",
	            dataIndex: 'id',
	            width: 100,
	            hidden:true
	        },{
	        	id:'name',
	            header: "当事人",
	            dataIndex: 'name',
	            width: 100
	        },{
	            header: "性质",
	            dataIndex: 'entityType',
	            width: 100
	        },{
	            header: "编号",
	            dataIndex: 'idNo',
	            width: 100
	        }],

	        // customize view config
	        viewConfig: {
	            forceFit:false,
	            enableRowBody:true,
	            showPreview:true,
	            getRowClass : function(record, rowIndex, p, store){
	                if(this.showPreview){
	                    return 'x-grid3-row-expanded';
	                }
	                return 'x-grid3-row-collapsed';
	            }
	        },
	        tbar: new Ext.Toolbar({
	        	items   : [
		            {
		            	xtype:'tbtext',
		                text: '当事人'
		            },
		            {
		            	id:'breakName',
		                xtype:'textfield',
		                width:'100'
		            },
		            {
		            	xtype:'tbtext',
		                text: '编号'
		            },
		            {
		            	id:'idNo',
		                xtype:'textfield',
		                width:'100'
		            },entityType,
		            {
		            	xtype:'button',
		                text: '查询',
		                handler:function(){
		                	store.load({params:{
		                	start:0, 
		                	limit:10,
		                	filter_LIKES_breakerName:$("#breakName").val(),
		                	filter_LIKES_idNo:$("#idNo").val(),
		                	filter_EQI_entityType:entityType.getValue()
		                	}});
		                }
		            },
		            {
		            	xtype:'button',
		                text: '增加',
		                handler:function(){
		                	if(inputWin==null){
			                	inputWin = new Ext.Window({
					               width:370,
					               closable:false,
								   modal :'true',
								   items:[simple],
								   bodyBorder :false,
								   border :false,
					               plain: true
					           });
		                	}
		                	simple.getForm().reset();
		                	inputWin.show();
		                }
		            }
		        ]
	        }),

	        // paging bar on the bottom
	        bbar: new Ext.PagingToolbar({
	            pageSize: 10,
	            store: store,
	            displayInfo: true,
	            displayMsg: '当前显示数据 {0} - {1} of {2}',
	            emptyMsg: "没有数据可以显示"
	        })
	    });

	    var grid2 = new Ext.grid.GridPanel({
	        width:250,
	        height:250,
	        ddGroup          : 'firstGridDDGroup',
	        enableDragDrop   : true,
	        store: store2,
	        trackMouseOver:true,
	        disableSelection:false,
	        loadMask: true,
	        stripeRows: true,
	        // grid columns
	        columns:[{
	            header: "id",
	            dataIndex: 'id',
	            width: 100,
	            hidden:true
	        },{
	            header: "当事人",
	            dataIndex: 'name',
	            width: 100
	        },{
	            header: "编号",
	            dataIndex: 'idNo',
	            width: 100
	        }],

	        // customize view config
	        viewConfig: {
	            forceFit:true,
	            enableRowBody:true,
	            showPreview:true,
	            getRowClass : function(record, rowIndex, p, store){
	                if(this.showPreview){
	                    return 'x-grid3-row-expanded';
	                }
	                return 'x-grid3-row-collapsed';
	            }
	        }
	    });
	    
   var win = null;
   var button = Ext.get('breakerNames');
   button.on('click', function(){
       if(win == null){
           win = new Ext.Window({
               applyTo:'chooseBreaker',
               title:'选择当事人',
               layout:'hbox',
               width:670,
               height:320,
               closable:false,
			   modal :'true',
               plain: true,
               items:[grid,grid2],
               buttons: [{
                   text:'选择',
                   handler: function(){
                   		var c = grid2.getStore().getCount();
                   		if(c==0){
                   			Ext.MessageBox.show({
					           title: '提示',
					           msg: '请选择当事人！',
					           buttons: Ext.MessageBox.OK,
					           icon: Ext.MessageBox.WARNING
					       });
					       return ;
                   		}
                   		var names = "";
                   		var ids = "";
                   		for(i=0;i<c;i++){
                   			names += grid2.getStore().getAt(i).data['name']+",";
                   			ids   += grid2.getStore().getAt(i).data['id']+",";
                   		}
                   		names = names.substring(0,names.length-1);
                   		ids = ids.substring(0,ids.length-1);
                   		//alert(names+"||"+ids);
                   		$("#breakerNames").val(names);
                   		$("#breakerIds").val(ids);
                   		win.hide();
              		}
               },{
                   text: '关闭',
                   handler: function(){
                       win.hide();
                      // $('#hello-win').html("");
                       //win = null;
                   }
               }]
           });
       }
       win.show(this);
       var firstGridDropTargetEl =  grid.getView().scroller.dom;
       var firstGridDropTarget = new Ext.dd.DropTarget(firstGridDropTargetEl, {
               ddGroup    : 'firstGridDDGroup',
               notifyDrop : function(ddSource, e, data){
                       var records =  ddSource.dragData.selections;
                       Ext.each(records, ddSource.grid.store.remove, ddSource.grid.store);
                       grid.store.add(records);
                       return true
               }
       });


       // This will make sure we only drop to the view scroller element
       var secondGridDropTargetEl = grid2.getView().scroller.dom;
       var secondGridDropTarget = new Ext.dd.DropTarget(secondGridDropTargetEl, {
               ddGroup    : 'secondGridDDGroup',
               notifyDrop : function(ddSource, e, data){
                       var records =  ddSource.dragData.selections;
                       Ext.each(records, ddSource.grid.store.remove, ddSource.grid.store);
                       grid2.store.add(records);
                       return true
               }
       });
       store.load({params:{start:0, limit:10}});
       //store2.removeAll();
   });
   
 
});